let EnumRoleType = {
    ADMIN: 'admin',
    DEFAULT: 'guest',
    DEVELOPER: 'developer',
};

let userPermission = {
    DEFAULT: {
        visit: ['1', '5', '6'],
        role: EnumRoleType.DEFAULT,
    },
    ADMIN: {
        visit: ['1', '2', '3', '4', '5', '6', '8','10','11','13'],
        role: EnumRoleType.ADMIN,
    },
    DEVELOPER: {
        visit: ['1', '3', '4', '5', '6'],
        role: EnumRoleType.DEVELOPER,
    },
};

let adminUsers = [
    {
        id: 0,
        username: 'admin',
        password: 'admin',
        permissions: userPermission.ADMIN,
    }, {
        id: 1,
        username: 'guest',
        password: 'guest',
        permissions: userPermission.DEFAULT,
    }, {
        id: 2,
        username: 'WHSJ',
        password: 'WHSJ',
        permissions: userPermission.DEVELOPER,
    },
];

module.exports = {

    [`POST /user/search`](req, res) {
        const { taken } = req.body;
        const user = adminUsers.filter(item => item.username === taken);
        if (user.length > 0) {
            res.status(203).json({
                success: true,
                message: '该用户已登录',
                user: user[0].username,
                permissions: user[0].permissions,
            })
        } else {
            res.status(200).json({
                success: false,
                message: '该用户还未登陆'
            })
        }
    },

    [`POST /user/login`](req, res) {
        const { username, password } = req.body;
        const user = adminUsers.filter(item => item.username === username);
        if (user.length < 0){
            res.status(200).json({
                success: false,
                message: '该用户不存在，请重新注册'
            });
        }
        if( user[0].password !== password ){
            res.status(200).json({
                success: false,
                message: '用户密码错误'
            });
        }

        const now = new Date();
        now.setDate(now.getDate() + 1);
        res.cookie('token', JSON.stringify({id: user[0].id, deadline: now.getTime()}), {
            maxAge: 900000,
            httpOnly: true,
        });
        res.status(200).json({
            success: true,
            message: 'Ok'
        });
    },

    [`POST /user/logout`](req, res) {
        res.clearCookie('token');
        res.status(200).json({
            success: true,
            message: '退出成功'
        })
    },

    [`POST /permission/list`] (req, res) {
        let { page, pageSize } = req.body;
        pageSize = pageSize || 10;
        page = page || 1;

        let newData = [];
        adminUsers.forEach((item)=>{
            let temp = {};
            temp.id = item.id;
            temp.username = item.username;
            temp.password = item.password;
            temp.userPermissions = item.permissions.visit;
            temp.role = item.permissions.role;
            newData.push(temp);
        });

        res.status(200).json({
            success: true,
            message: '获取权限成功',
            data: newData.slice((page - 1) * pageSize, page * pageSize),
            total: newData.length,
            page: parseInt(page,10),
            pageSize: parseInt(pageSize,10)
        })
    },

    [`POST /permission/update`] (req, res) {
        const { values ,id } = req.body;
        adminUsers = adminUsers.map((item) => {
            if (item.id === parseInt(id,10)) {
                item.permissions.visit = values;
                return item;
            }
            return item
        });
        res.status(200).json({
            success: true,
            message: '修改权限成功',
            data: adminUsers
        })
    },

    [`POST /permission/get`] (req, res) {
        const { id } = req.body;
        const permissions = adminUsers.filter(item => item.id === parseInt(id,10));
        res.status(200).json({
            success: true,
            message: '修改权限成功',
            data: permissions[0].permissions.visit
        })
    },

};